import 'package:http/http.dart';
import 'dart:convert';
import "package:flutter/material.dart";
import 'package:android_studio_f/generated/json/base/credential_list_item_entity.dart';

import '../routes/routers.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          actions: [
            IconButton(
                onPressed: () {
                  print("搜索");
                },
                icon: Icon(Icons.search)),
            IconButton(
                onPressed: () {
                  print("更多选项");
                },
                icon: Icon(Icons.menu))
          ],
          title: Text("已分享列表"),
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        ),
        body: ListContent(),
        floatingActionButton: FloatingActionButton(
            onPressed: () {
              Navigator.pushNamed(context, editCredentialName);
            },
            child: Icon(Icons.add)));
  }
}

class ListContent extends StatefulWidget {
  const ListContent({super.key});

  @override
  State<ListContent> createState() => _ListState();
}

class _ListState extends State<ListContent> {
  late Future<List<CredentialListItemEntity>> credentials;

  Future<List<CredentialListItemEntity>> fetchList() async {
    final response =
        // await get(Uri.parse("http://192.168.75.24:3000/api/resume"));
        await get(Uri.parse("https://xjm.deno.dev/api/resume"));
    if (response.statusCode == 200) {
      final list = await jsonDecode(
          const Utf8Decoder().convert(response.body.runes.toList()));
      return List<CredentialListItemEntity>.from(
          list.map((e) => CredentialListItemEntity.fromJson(e)));
    } else {
      throw Exception('加载凭证列表出现错误');
    }
  }

  void handleDelete(CredentialListItemEntity info) async {
    showDialog(
        context: context,
        builder: (ctx) {
          return AlertDialog(
            title:const Text(
              "删除凭据",
              style: TextStyle(color: Colors.red),
            ),
            content: Text("删除分享给[${info.corporateName}]的凭据?"),
            actions: [
              TextButton(
                  onPressed: () {

                    Navigator.pop(context,false);
                  },
                  child: Text(
                    "确定",
                    style: TextStyle(color: Colors.redAccent),
                  )),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context,false);
                  },
                  child: Text(
                    "取消",
                    style: TextStyle(color: Colors.blue),
                  )),
            ],
          );
        }).then((value) {
          print(value);
    });


  }

  @override
  void initState() {
    super.initState();
    credentials = fetchList();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<CredentialListItemEntity>>(
      future: credentials,
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return Text("${snapshot.error}");
          }
          // return ListView.builder(itemBuilder: (ctx,index)=>ListTile(title:Text(ctx.)))
          return ListView(
            children: snapshot.data!
                .map((e) => ListTile(
                      title: Text(e.corporateName),
                      subtitle: Text(e.createAt),
                      trailing: Icon(Icons.keyboard_arrow_right),
                      onTap: () {
                        // 点击前往详情
                        Navigator.pushNamed(context, editCredentialName,
                            arguments: e);
                      },
                      onLongPress: () => handleDelete(e),
                    ))
                .toList(),
          );
        } else {
          return const CircularProgressIndicator();
        }
      },
    );
  }
}
